define(function(require,exports,module) {
 //    require('./plug/hammer.min');
 //    require('./plug/swiper.jquery.min');
	// require('./plug/lazyload_echo');
	//require('shop');
    var myPub = require('./public');

    var oHeader = $('.js_header');              //头部
    var oFooter = $('.js_footer');              //底部
    var oContain = $('.js_contain');			//主体
    var oHScroll = $('.js_HScroll');			//滚动产品
    var oGoods_showOne = $('.js_goods_showOne');	//一排产品
    var oGoods_showTwo = $('.js_goods_showTwo');	//并排产品

	oHeader.on("touchmove",function(e) {e.preventDefault()});
    oFooter.on("touchmove",function(e) {e.preventDefault()});
    //oSearchBox.on("touchmove",function(e) {e.preventDefault()});

    var oHScroll = $('.js_HScroll');		//水平滚动 导航和产品
    var oHScroll_Ul = oHScroll.find('ul');
    var oHScroll_Li = oHScroll.find('li');
	SetUlWidth();
	$(window).resize(function() { SetUlWidth(); });

	//水平滚动
    oHScroll.on('scroll',function() {
    	//水平懒加载
    	myPub.throttle(myPub.lazLoadingImgEcho, window);
    	//设置水平滚动位置
    	sessionStorage.setItem("index_oHScroll", oHScroll.scrollLeft());
    });

    //设置并排产品的图片高度
    myPub.SetImgHeight(oGoods_showTwo);

    function SetUlWidth() {
    	 //水平滚动li的宽度
	    var oHScroll_Li_W = oHScroll_Li.outerWidth(true);
	    oHScroll_Ul.css({
	    	'width' : oHScroll_Li_W * oHScroll_Li.length
	    });
    }



    var limit = 20;
	var pageNo,begin,end;


	//产品懒加载  异步加载 调用这个
    myPub.lazLoadingImgEcho()

    var goodsList = [];
	module.exports = {
        init:function(data){
        	goodsList = data;
        	//console.log(goodsList)
        	Swiper_indexAD();		//广告轮播
        	HistoryData();			//定位历史数据和页面位置
        	HistoryX(); 			//水平定位
        	//HistoryScrollY()		//页面滚动位置
        }
    };

	$('#more').show();
	//显示更多
	var  oMoreBtn = $('.js_moreBtn'); 			//加载更多按钮
	//var  oLoadingBtn = $('.js_loadingBtn'); 	//加载中
	var loadTime = null;
	var loadGoHeight = oGoods_showTwo.find('li').height();

	//水平定位
    function HistoryX() {
    	if(sessionStorage.getItem('index_oHScroll') !== null) {
	    	var _HDis = parseInt( sessionStorage.getItem('index_oHScroll') )
	    	oHScroll.scrollLeft( _HDis )
	    }

    }

    //页面滚动历史记录
    function HistoryScrollY() {

    	if(sessionStorage.getItem("index_scrollTop") !== null  ) {
	    	var _scrollTop = parseInt( sessionStorage.getItem("index_scrollTop") )
	    	$(window).scrollTop( _scrollTop )
	    }
    }

	//定位历史数据和页面位置
	function HistoryData() {
		if(sessionStorage.getItem("index_pageNo") !== null  ) {
	    	var _pageNo = parseInt( sessionStorage.getItem("index_pageNo") )
	    	pageNo = _pageNo;
	    	begin = limit;
	    	sHtmlData(function() {
				HistoryScrollY()
			})
	    }
		else {
			pageNo = 0;
			begin = pageNo*limit;
			HistoryScrollY()
		}


	}

	//获取数据并编辑
	function sHtmlData(callBack) {
		//console.log('pageNo:'+pageNo)
		//begin = pageNo*limit;
		end = (pageNo+1)*limit;

		if(end >= goodsList.length){
			end = goodsList.length;
		}

		var html="";

		//console.log('begin:' + begin)
		//console.log('end:' + end)

		for(var i = begin; i<end; i++){
			//console.log(goodsList[i]+"i:"+i);
			html+='<li>'+
					'<a class="" href="/shop/goods/view/'+goodsList[i].goodsSn+'">'+
					    '<figure>'+
					        '<img class="js_lazyImg" data-echo="'+myPub.httpImageUrl + goodsList[i].showImage + '" src="/static_v3/image/loadImg.jpg">'+
					        //'<img class="js_lazyImg" data-echo="'+myPub.httpImageUrl + goodsList[i].showImage + '">'+
					        '<i class="tip-th" style="display: none;"></i>'+
					    '</figure>'+
					    '<aside class="priceArea ">'+
					    	'<div class="priceArea_name">'+
					    		'<h4>'+goodsList[i].name+'</h4>'+
					    		'<p class="smallFont_tip">'+goodsList[i].title+'</p>'+
					    	'</div>'+
					    	'<div class="priceArea_price">'+
				            	'<p class="HBox BoxCenter priceAreaBox">'+
				                        '<i>￥</i>'+
				                        '<span class="goodsPrice">'+goodsList[i].salePrice+'</span>'+
				                        '<span class="goodsWeight Bflex1">/'+goodsList[i].productName+'</span>'+
				                '</p>'+
			            	'</div>'+
					    '</aside>'+
					'</a>'+
					'<p class="addCar js_addCar" pId="'+goodsList[i].productId+'"><em class="icon-shopcar_small"></em></p>'+
				'</li>';


		}

    	oGoods_showTwo.find('ul').append(html);
    	if(callBack)callBack(begin,end)
	}

	//点击更多
	oMoreBtn.hammer().on('tap',function() {
		var _this = $(this);
		//显示加载中
		_this.hide().siblings('.js_loadingBtn').show().find('.icoStyle').addClass('myRotateRefresh');


		clearTimeout(loadTime);
		loadTime = setTimeout(function () {
			//隐藏加载中
			_this.show().siblings('.js_loadingBtn').hide().find('.icoStyle').removeClass('myRotateRefresh');

			pageNo++;
			begin = pageNo*limit;

			//设置显示了几次
			sessionStorage.setItem("index_pageNo", pageNo);

			sHtmlData(function(start,end) {
				//产品懒加载  异步加载刷新
				myPub.lazLoadingImgEcho()
				myPub.SetImgHeight(oGoods_showTwo);

				if((end-start)<limit){
					$('#noMore').show();
					$('#more').hide();
				}

			})

		},300)

		myPub.HamstopPropaga();
	})

	//定位页面滚动距离
	$(window).unload(function(){
    	sessionStorage.setItem("index_scrollTop", $(window).scrollTop());
	});



	//广告轮播
	var sTime = null;
    var oSilderImg = $('.js_silderImg');        //广告轮播框
    var oBNext = $('.js_BNext');                //下一个
    var oBPrev = $('.js_BPrev');                //上一个
    var oPagin = $('.js_pagin');                //个数点

    function Swiper_indexAD(){
    	var indexSwiperAD = oSilderImg.swiper({
      	nextButton: oBNext,
    	    prevButton: oBPrev,
    	    pagination: oPagin,
    	    paginationClickable: false,
    	    preloadImages: false,
    	    lazyLoading: true,
    	    autoplay: 4000,
    	    onTouchEnd: function (swiper) {
    	        clearTimeout(sTime);
    	        sTime = setTimeout(function () {
    	        	indexSwiperAD.startAutoplay();
    	        }, 4000);
    	    },
    	});
    }


    function SetSession() {
    	sessionStorage.setItem("index_limit", pagearr);
    	sessionStorage.setItem("index_pageNo", falgarr);
    }

});